#include "FlashBang.h"

#define TIMETOWAIT 180;

bool FlashBang::checkShieldForHit(RiotShield* shield){
	double lx, ly, hx, hy;
	shield->getBoundingBox(&lx, &ly, &hx, &hy);
	double mlx = myPos.x -20;
	double mly = myPos.y - 20;
	double mhx = myPos.x + 20;
	double mhy = myPos.y + 20;
	if(lx <= mhx && mlx <= hx && ly <= mhy && mly <= hy){
		return 1;
	}
	return 0;
}

char *grenadeFile = "Flashbang.mesh";

FlashBang::FlashBang(char* name, Ogre::SceneManager* sm, Ogre::SceneNode* toAddTo, ScoreCard* scoreboard, RiotShield* playerS){
	Ogre::Entity* grenade;
	grenade = sm->createEntity(name, grenadeFile);
	myNode = toAddTo->createChildSceneNode();
	Ogre::SceneNode* tmpNode = myNode->createChildSceneNode();
	tmpNode->setScale(20,20,20);
	tmpNode->pitch(Ogre::Degree(90));
	tmpNode->attachObject(grenade);
	myPos.z = POLICEZ;
	myNode->setPosition(myPos.x, myPos.y, myPos.z);
	randomizeVelocity(&myPos.vx, &myPos.vy);
	myPos.vz = -1 * FBINITV;
	theScore = scoreboard;
	playerShield = playerS;
	selfPhysics = 1;
	shieldBounce = NULL;
	shieldBounce = Mix_LoadWAV("sounds/RiotShieldHit.wav");
	wallBounce = Mix_LoadWAV("sounds/FlashbangBounce.wav");
	myEyesAHHH = Mix_LoadWAV("sounds/InmatesFlashed2.wav");
	explosion = Mix_LoadWAV("sounds/FlashbangExplode.wav");
	pulledPin = Mix_LoadWAV("sounds/pinpull.wav");
	youFailed = Mix_LoadWAV("sounds/InmatesLaughing2.wav");
	youreFired = Mix_LoadWAV("sounds/WardenAngry.wav");
	grenadesLeft = 4;
	gameOver = 0;
	playPinPull();
	laughDelay = 240;
	chastiseDelay = 480;
	soundLive = 1;
}

void FlashBang::updateState(){
	if(gameOver){
		if(laughDelay > 0){
			laughDelay--;
			if(laughDelay <= 0){
				playGameOver();
			}
		}
		if(chastiseDelay > 0){
			chastiseDelay--;
			if(chastiseDelay <= 0){
				playWardenAngry();
			}
		}
		return;
	}
	if(timeToWait > 0){
		timeToWait--;
		if(timeToWait <= 0){
			myPos.z = POLICEZ;
			myPos.x = 0;
			myPos.y = 0;
			myPos.vz = -1 * FBINITV;
			randomizeVelocity(&(myPos.vx), &(myPos.vy));
			playPinPull();
		}
		return;
	}
	//reconcile physics with myself
	//
	//
	//
	if(selfPhysics){
		double grav = -2;
		myPos.accelerate(0,grav,0);
		myPos.updatePosition();
		myNode->setPosition(myPos.x, myPos.y, myPos.z);
		if(myPos.x > (MAXX - 20) && myPos.vx > 0){
			myPos.x = MAXX-20;
			myPos.vx = -(myPos.vx);
			playWallContact();
		}
		if(myPos.x < (MINX + 20) && myPos.vx < 0){
			myPos.x = MINX+20;
			myPos.vx = -(myPos.vx);
			playWallContact();
		}
		if(myPos.y > (MAXY - 20) && myPos.vy > 0){
			myPos.y = MAXY-20;
			myPos.vy = -(myPos.vy);
			playWallContact();
		}
		if(myPos.y < (MINY + 20) && myPos.vy < 0){
			myPos.y = MINY+40;
			myPos.vy = -(myPos.vy);
			playWallContact();
		}
		if(myPos.z > (POLICEZ-20) && myPos.vz > 0){
			if(checkShieldForHit(playerShield)){
				myPos.vz = -(myPos.vz + 5);
				//randomizeVelocity(&(myPos.vx), &(myPos.vy));
				playShieldContact();
			}
			else{
				//set up wait state
				playOhGodMyEyes();
				grenadesLeft = grenadesLeft-1;
				if(grenadesLeft<=0){
					gameOver = 1;
				}
				timeToWait = TIMETOWAIT;
			}
		}
		if(myPos.z < (INMATESZ+20) && myPos.vz < 0){
			myPos.vz = -(myPos.vz);
			makeScore();
		}
	}
}

void FlashBang::alertToInput( const InputSet& input ){
	if(input.get(IS_B)){
		if(!lock){
			soundLive = !(soundLive);
		}
		lock = 1;
	}
	else{
		lock = 0;
	}
}

void FlashBang::randomizeVelocity(double* xv, double* yv){
	*xv = FBINITV * ((2*((1.0 * rand()) / RAND_MAX))-1);
	*yv = (FBINITV * ((2*((1.0 * rand()) / RAND_MAX))-1));
}

void FlashBang::playPinPull(){
	if(soundLive){
		Mix_PlayChannel(-1, pulledPin, 0);
	}
}

void FlashBang::playWallContact(){
	if(soundLive){
		Mix_PlayChannel(-1, wallBounce, 0);
	}
}
void FlashBang::playShieldContact(){
	if(soundLive){
		Mix_PlayChannel(-1, shieldBounce, 0);
	}
}

void FlashBang::makeScore(){
	theScore->addScore(SCORE);
}

void FlashBang::playGameOver(){
	if(soundLive){
		Mix_PlayChannel(-1, youFailed, 0);
	}
}

void FlashBang::playWardenAngry(){
	if(soundLive){
		Mix_PlayChannel(-1, youreFired, 0);
	}
}

void FlashBang::playScoreSound(){}

void FlashBang::playOhGodMyEyes(){
	if(soundLive){
		Mix_PlayChannel(-1, myEyesAHHH, 0);
		Mix_PlayChannel(-1, explosion, 0);
	}
}
void FlashBang::shieldMissed(){}
